package com.cpit.platform.dto;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.JoinColumn;
@Entity
public class Role implements Serializable {

	@Id
	@GeneratedValue
	private Integer id;

	@Column(nullable = false, length = 50)
	private String name;

	@Column(nullable = false, length = 500)
	private String description;

	@ManyToMany(cascade={CascadeType.DETACH}, fetch=FetchType.EAGER )
	@JoinTable(name="RoleToAuthority",
			joinColumns={
					@JoinColumn(name="roleId",referencedColumnName="id", unique=false)},
			inverseJoinColumns={
					@JoinColumn(name="authId",referencedColumnName="id", unique=false)}
	)
	private List<Authority> authorities;

	@Transient
	private String authoritiesContent;
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Authority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}

	public String getAuthoritiesContent() {
		StringBuilder sb = new StringBuilder();
		if(authorities != null && authorities.size() > 0) {
			for(Authority a : this.authorities) {
				sb.append(a.getName());
				sb.append(";");
			}
		}
		this.authoritiesContent = sb.toString();
		return authoritiesContent;
	}

	public void setAuthoritiesContent(String authoritiesContent) {
		this.authoritiesContent = authoritiesContent;
	}

	@Override
	public String toString() {
		return "Role [id=" + id + ", name=" + name + ", description=" + description + ", authorities=" + authorities
				+ ", authoritiesContent=" + authoritiesContent + "]";
	}

}